package org.xiaoyu.rpc.common.url;

import lombok.*;

import java.io.Serializable;

/**
 * Created By Have
 * 2021/7/12 21:31
 */

@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
@Getter
@Setter
public class URL implements Serializable {
    private String version;
    private String group;
    private String serviceName;
    private String host;
    private int port;
    private String path;
    private Float weight;
    private Integer replicaNumber;
    private Class<?> interfaceClass;
    private Boolean async = false;

    public boolean isAsync() {
        return async;
    }

    public String getServiceUniqueName() {
        StringBuilder sb = new StringBuilder();
        sb.append(host);
        sb.append(":");
        sb.append(port);
        sb.append("/");
        sb.append(serviceName);
        sb.append(":");
        sb.append(group);
        sb.append(":");
        sb.append(version);
        return sb.toString();
    }

    public String getAllPath() {
        StringBuilder sb;
        if (path.startsWith("/")) {
            sb =  new StringBuilder();
        } else {
            sb = new StringBuilder("/");
        }
        sb.append(path);
        sb.append("/");
        sb.append(serviceName);
        sb.append("/");
        sb.append(group);
        sb.append("/");
        sb.append(version);
        sb.append("/");
        sb.append(host);
        sb.append(":");
        sb.append(port);
        return sb.toString();
    }
    public String getServicePath() {
        StringBuilder sb;
        if (path.startsWith("/")) {
            sb =  new StringBuilder();
        } else {
            sb = new StringBuilder("/");
        }
        sb.append(path);
        sb.append("/");
        sb.append(serviceName);
        sb.append("/");
        sb.append(group);
        sb.append("/");
        sb.append(version);
        return sb.toString();
    }

    public void setPath(String path) {
        if (!path.startsWith("/")) {
            this.path = "/" + path;
        } else {
            this.path = path;
        }
    }

    public String getAddress() {
        return "/" + host + ":" + port;
    }

}
